package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    //分类管理分页查询
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        // 开始分页查询
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);

        long total = page.getTotal();
        List<Category> records = page.getResult();
        return new PageResult(total, records);
    }

    //新增分类
    public void save(CategoryDTO categoryDTO) {

        log.info("新增分类 {}",categoryDTO);
        Category category = new Category();
        //属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        //默认状态为禁用
        category.setStatus(StatusConstant.DISABLE);
        //设置创建时间，修改时间，创建人，修改人  --- 交给切面类了
        //category.setCreateTime(LocalDateTime.now());
        //category.setUpdateTime(LocalDateTime.now());
        //category.setCreateUser(BaseContext.getCurrentId());
        //category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.insert(category);
    }

    //修改分类
    public void update(CategoryDTO categoryDTO) {
        log.info("修改分类 {}",categoryDTO);
        Category category = new Category();
        //
        BeanUtils.copyProperties(categoryDTO, category);
        //category.setName(categoryDTO.getName());
        //category.setSort(categoryDTO.getSort());
        //category.setType(categoryDTO.getType());

        //设置修改时间，修改人  --- 交给切面类了
        //category.setCreateUser(BaseContext.getCurrentId());
        //category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.update(category);
    }

    //启用禁用分类
    public void startOrStop(Integer status, long id) {
        Category category = Category.builder()//代码简洁、可读性强，并且只设置需要更新的字段，是一种良好的编程实践
                .id(id)
                .status(status)
                //.updateTime(LocalDateTime.now())  --- 交给切面类了
                //.updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.update(category);
    }

    //根据id删除分类
    public void deleteById(long id) {

        log.info("根据id删除分类：{}",id);
        Integer dishCount = dishMapper.countByCategoryId(id);
        //判断当前分类是否关联了菜品，如果已经关联，则无法删除
        if(dishCount > 0)
        {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        Integer setmealCount = setmealMapper.countByCategoryId(id);
        //判断当前分类是否关联了套餐，如果已经关联，则无法删除
        if(setmealCount > 0)
        {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //删除分类数据
        categoryMapper.deleteById(id);
    }

    public List<Category> list(Integer type) {
        List<Category> list = categoryMapper.list(type);
        return list;
    }
}